package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffCostBListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostB;
import cn.ffcs.uec.outStaff.model.UecOutStaffWork;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecOutStaffCostBListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	
	/**
	 * 对应页面上的bean
	 */
	private UecOutStaffCostBListboxBean bean = new UecOutStaffCostBListboxBean();
	/**
	 * 外包人员费用子表实体
	 */
	private UecOutStaffCostB uecOutStaffCostB;
	/**
	 * 外包人员费用子表实体
	 */
	private UecOutStaffCostB qryUecOutStaffCostB;
	
	private UecOutStaffWork uecOutStaffWork;

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffCostBListboxExt() {
		Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_COSTB_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_COSTB_QUERY, this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_COSTB_QUERY_RESPONSE);

		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_COSTB, this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_COSTB_RESPONSE);
		
	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		 this.setButtonValid(false, false,false,false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecOutStaffCostBList() throws Exception {
		if (this.bean.getUecOutStaffCostBListbox().getSelectedCount() > 0) {
			uecOutStaffCostB = (UecOutStaffCostB) bean.getUecOutStaffCostBListbox().getSelectedItem()
					.getValue();
			if (StrUtil.isEmpty(uecOutStaffCostB.getPkPsnWork())) {
				this.setButtonValid(true, true, true, true);
			}else {
				this.setButtonValid(false, false, false, true);
			}
			Events.postEvent("onSelectUecOutStaffCostB", this, uecOutStaffCostB);
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffCostBQueryResponse(ForwardEvent event) {
		uecOutStaffWork = (UecOutStaffWork) event.getOrigin().getData();
		if (null != uecOutStaffWork) {
			if (!StrUtil.isEmpty(uecOutStaffWork.getPkPsnWork())) {
				this.setButtonValid(false, false,false,true);
			}else{
				this.setButtonValid(true, true,true,true);
			}
			qryUecOutStaffCostB = new UecOutStaffCostB();
			this.onQueryUecOutStaffCostB();
		}else {
			this.setButtonValid(false, false,false,false);
			//ZkUtil.showError("请选择工作记录", "提示信息");
		}
	}
	/**
	 * 查询按钮
	 */
	public void onQueryUecOutStaffCostB() {
		clearVariable();
		UecOutStaffCostB objUecOutStaffCostB = UecOutStaffCostB.newInstance();
		PubUtil.fillPoFromBean(bean, objUecOutStaffCostB);
		qryUecOutStaffCostB = objUecOutStaffCostB;
		
		qryUecOutStaffCostB.setOutStaffWorkId(uecOutStaffWork.getId());

		this.bean.getUecOutStaffCostBListboxPaging().setActivePage(0);
		this.onQueryUecOutStaffCostBList();
		
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecJob() {
		
		ListboxUtils.clearListbox(this.bean.getUecOutStaffCostBListbox());
		this.bean.getUecOutStaffCostBListboxPaging().setTotalSize(1);
	}
	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecOutCostBResponse(ForwardEvent event){
		onCleanUecOutStaffCostBResponse();
	}
	
	public void onCleanUecOutStaffCostBResponse() {
		clearVariable();
		uecOutStaffWork = null;
		ListboxUtils.clearListbox(this.bean.getUecOutStaffCostBListbox());
		this.bean.getUecOutStaffCostBListboxPaging().setTotalSize(1);
		
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canAdd,boolean canEdit, boolean candel, boolean canView) {
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
		this.bean.getViewButton().setDisabled(!canView);
	}



	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */			
	public void onAddUecOutStaffCostB() throws Exception {
		openUecOutStaffCostBEditWin("add");
	}
	/**
	 * 修改按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onEditUecOutStaffCostB() throws Exception {
		if (this.uecOutStaffCostB != null) {
			openUecOutStaffCostBEditWin("edit");
			this.onQueryUecOutStaffCostB();
		} else{
			ZkUtil.showError("请选择你要修改的记录", "提示信息");
		}
		
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onViewUecOutStaffCostB() throws Exception {
		if (this.uecOutStaffCostB != null) {
			openUecOutStaffCostBEditWin("view");
		} else{
			ZkUtil.showError("请选择你要查看的记录", "提示信息");
		}
		
	}
	/**
	 * openUacStaffInfoWin
	 * 
	 * @param opType
	 */
	private void openUecOutStaffCostBEditWin(String opType) {
		if (null != uecOutStaffWork) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", opType);
			map.put("uecOutStaffWork", uecOutStaffWork);
			map.put("uecOutStaffCostB", uecOutStaffCostB);
			if ("edit".equals(opType)) {
				
				map.put("uecOutStaffCostBId", uecOutStaffCostB.getId());
			}
			
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_COSTB_EDIT_ZUL, this, map);
			try {
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecOutStaffCostB = (UecOutStaffCostB) event.getData();
							onQueryUecOutStaffCostBList();
						}
					}
				});
			} catch (SuspendNotAllowedException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 删除按钮
	 */
	public void onDelUecOutStaffCostB() {
		if (this.uecOutStaffCostB != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecOutStaffCostB.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecOutStaffCostBListbox(),
								uecOutStaffCostB, "del");
						uecOutStaffCostB = null;
						setButtonValid(true,false, false,false);
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutStaffCostBListboxPaging() {
		this.onQueryUecOutStaffCostBList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecOutStaffCostB = null;
		qryUecOutStaffCostB = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutStaffCostBList() {
		if (null != qryUecOutStaffCostB) {
			PageInfo pageInfo = uecOutStaffManager.queryPageInfoByUecOutStaffCostB(
					uecOutStaffWork, this.bean.getUecOutStaffCostBListboxPaging()
							.getActivePage() + 1, this.bean
							.getUecOutStaffCostBListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			/*if (list.getSize() == 0) {
				uecOutStaffCostB = UecOutStaffCostB.newInstance();
				uecOutStaffCostB.setPkPsnCostH("-1");
				Events.postEvent("onSelectUecOutStaffCostB", this, uecOutStaffCostB);
			}*/
			this.bean.getUecOutStaffCostBListbox().setModel(list);
			this.bean.getUecOutStaffCostBListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}

}
